package com.zp.imagecompresspractice.View;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zp.imagecompresspractice.R;

/**
 * Created by izp on 2017/7/20.
 */

public class TabShapeActivity extends AppCompatActivity {
    private ViewPager mViewPager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_shape);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);//把toolBar转换成actionBar
        FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return PageFragment.newInstance(position);
            }

            @Override
            public int getCount() {
                return 7;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return "position"+position;
            }
        };
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mViewPager.setAdapter(fragmentPagerAdapter);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);

        ShapeIndicatorView shapeIndicatorView = (ShapeIndicatorView) findViewById(R.id.shapeIndicator);
        tabLayout.setupWithViewPager(mViewPager);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);//tab可滑动

        shapeIndicatorView.bindWithTabLayout(tabLayout);
        shapeIndicatorView.bindWithViewPager(mViewPager);
    }

    public static class PageFragment extends Fragment{
        private static final String SECTION_ARGUMENT = "section_number";
        public PageFragment() {
        }

        public static PageFragment newInstance(int position){
            PageFragment pageFragment = new PageFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(SECTION_ARGUMENT,position);
            pageFragment.setArguments(bundle);
            return pageFragment;
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            TextView rootView = new TextView(getActivity());
            rootView.setText(getString(R.string.position_format,getArguments().getInt(SECTION_ARGUMENT,0)));
            rootView.setGravity(Gravity.CENTER);
            return rootView;
        }
    }
}
